package org.road0329.dlt698.axdr;


import org.road0329.dlt698.ReverseByteArrayInputStream;
import org.road0329.dlt698.ReverseByteArrayOutputStream;
import org.road0329.dlt698.asn1.axdr.AxdrEnum;
import org.road0329.dlt698.asn1.axdr.AxdrType;

import java.io.IOException;

public class FollowReport extends AxdrType {
    private byte[] dataCode;
    private Choice choice;
    private SubSeqOfResultNormal resultNormal;
    private SubSeqOfResultRecord resultRecord;

    public FollowReport() {
        dataCode = null;
        choice = Choice._ERR_NONE_SELECTED;
        resultNormal = null;
        resultRecord = null;
    }

    public void set_dataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setResultNormal(SubSeqOfResultNormal resultNormal) {
        resetChoices();
        choice = Choice.RESULT_NORMAL;
        this.resultNormal = resultNormal;
    }

    public void setResultRecord(SubSeqOfResultRecord resultRecord) {
        resetChoices();
        choice = Choice.RESULT_RECORD;
        this.resultRecord = resultRecord;
    }

    public int decode(ReverseByteArrayInputStream input) throws Exception {
        int codeLength = 0;
        AxdrEnum choosen = new AxdrEnum();
        choosen.setConst();
        codeLength += choosen.decode(input);
        resetChoices();
        choice = Choice.getEnumByValue((int) choosen.getValue());
        if (choice == Choice.RESULT_NORMAL) {
            resultNormal = new SubSeqOfResultNormal();
            codeLength += resultNormal.decode(input);
            return codeLength;
        } else if (choice == Choice.RESULT_RECORD) {
            resultRecord = new SubSeqOfResultRecord();
            codeLength += resultRecord.decode(input);
            return codeLength;
        }
        throw new Exception("Error decoding AxdrChoice: Identifier matched to no item.");
    }

    public int encode(ReverseByteArrayOutputStream output) throws Exception {
        if (dataCode != null) {
            for (int i = dataCode.length - 1; i >= 0; i--) {
                output.write(dataCode[i]);
            }
            return dataCode.length;
        }
        if (choice == Choice._ERR_NONE_SELECTED) {
            throw new Exception("Error encoding AxdrChoice: No item in choice was selected.");
        }
        int codeLength = 0;
        if (choice == Choice.RESULT_RECORD) {
            if (resultRecord != null) {
                codeLength += resultRecord.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(2);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new Exception("resultRecord is null");
            }
        } else if (choice == Choice.RESULT_NORMAL) {
            if (resultNormal != null) {
                codeLength += resultNormal.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(1);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new Exception("resultNormal is null");
            }
        }
        throw new Exception("Error encoding AxdrChoice: No item in choice was encoded.");
    }

    public void encodeAndSave(int encodingSizeGuess) throws Exception {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        encode(revOStream);
        dataCode = revOStream.getArray();
    }

    public Choice getChoiceIndex() {
        return choice;
    }

    public void resetChoices() {
        choice = Choice._ERR_NONE_SELECTED;
        resultNormal = null;
        resultRecord = null;
    }

    public String toString() {
        if (choice == Choice.RESULT_RECORD) {
            if (resultRecord != null) {
                return "choice: {resultRecord: " + resultRecord + "}";
            } else {
                return "choice is resultRecord but resultRecord is null";
            }
        } else if (choice == Choice.RESULT_NORMAL) {
            if (resultNormal != null) {
                return "choice: {resultNormal: " + resultNormal + "}";
            } else {
                return "choice is resultNormal but resultNormal is null";
            }
        } else {
            return "unknown";
        }
    }

    enum Choice {
        _ERR_NONE_SELECTED(-1),
        RESULT_NORMAL(1),
        RESULT_RECORD(2);

        private final int value;

        Choice(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }

        public static Choice getEnumByValue(int value) {
            for (Choice item : Choice.values()) {
                if (value == item.getValue()) {
                    return item;
                }
            }
            return null;
        }
    }
}
